package com.etc.my.controller;


import com.etc.my.dto.MyMessage;
import com.etc.my.dto.PageDto;
import com.etc.my.dto.SellDto;
import com.etc.my.entity.Sell;
import com.etc.my.entity.User;
import com.etc.my.service.SellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class SellController {
    @Autowired
    private SellService ss;

    public SellService getSs() {
        return ss;
    }

    public void setSs(SellService ss) {
        this.ss = ss;
    }

    @RequestMapping("/getWantSellListByUserIdOfPage")
    @ResponseBody
    public MyMessage getWantSellListByUserIdOfPage(HttpSession session, Integer page, Integer size){
        MyMessage message = new MyMessage();
        //获取登录的用户
        User user = (User) session.getAttribute("loginuser");


        if(user!=null){
            //判断是否有取到值
            if(page==null||page<=0){
                page=1;
            }

            if(size==null||size<=0){
                size=3;
            }
            //获取总条数
            Integer allCount = ss.getWantSellCountByUserId(user.getUser_id()).getAllCount();
            if(allCount>=0&&allCount!=null){
                Integer maxPage = (allCount%size==0)?(allCount/size):(allCount/size+1);
                //页数条数上限容错
                page=page>maxPage?maxPage:page;
                size=size>3?3:size;
                //页数条数下限容错
                page=page<=0?1:page;
                size=size<=0?3:size;

                PageDto pd = new PageDto();
                pd.setPage(page);
                pd.setSize(size);

                //调用方法查询
                List<Sell> pushList = ss.getWantSellListByUserIdOfPage(user.getUser_id(),(page-1)*size,size);
                if(pushList.size()>0&&pushList!=null){
                    message.setFlag(1);
                    pd.setMaxPage(maxPage);
                    pd.setAllCount(allCount);
                    pd.setOlist(pushList);
                    message.setData(pd);
                }else{
                    message.setFlag(0);
                    message.setInfo("查无此纪录");
                }
            }else{
                message.setFlag(0);
                message.setInfo("查无此纪录");
            }

        }else{
            message.setFlag(0);
            message.setInfo("您尚未登录请先登录!");
        }
        System.out.println(message);
        return message;
    }

    @RequestMapping("/addSellCar")
    public void addSellCar(SellDto sellDto, HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        //处理地址
        if (sellDto.getSell_county().equals("市辖区")||sellDto.getSell_address().equals("")){
            //不做操作
            sellDto.setSell_address(null);
        }else {
            sellDto.setSell_address(sellDto.getSell_province()+"省"+sellDto.getSell_city()+sellDto.getSell_county()+sellDto.getSell_address());
        }

        //处理车辆品牌车型  品牌/车型
        if(sellDto.getSell_carname()!=null&&!sellDto.getSell_carname().equals("")){
            String[] carname = sellDto.getSell_carname().split("/");
            sellDto.setSell_carname(carname[0]);
            sellDto.setSell_cardept(carname[1]);
        }
        if (ss.addSellCar(sellDto)){
            pw.print("<script>alert('添加成功');location.href='index.jsp'</script>");
        }else {
            pw.print("<script>alert('添加失败');history.go(-1)</script>");
        }
        pw.flush();
        pw.close();
    }

    /**
     * 取消卖车订单预约
     * @param sell_id
     * @param response
     * @throws Exception
     */
    @RequestMapping("/CancelSellCar")
    public void CancelSellCar(Integer sell_id,HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        boolean result = ss.CancelSellCar(sell_id);
        if(result){
            pw.print("<script>alert('删除成功');location.href='mywantsellcar.jsp'</script>");
        }else {
            pw.print("<script>alert('删除失败');history.go(-1)</script>");
        }
    }

    @RequestMapping("/updateSellCar")
    public void updateSellCar(SellDto sellDto, HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        //处理地址
        if (sellDto.getSell_county().equals("市辖区")||sellDto.getSell_address().equals("")){
            //不做操作
            sellDto.setSell_address(null);
        }else {
            sellDto.setSell_address(sellDto.getSell_province()+"省"+sellDto.getSell_city()+sellDto.getSell_county()+sellDto.getSell_address());
        }

        //处理车辆品牌车型  品牌/车型
        if(sellDto.getSell_carname()!=null&&!sellDto.getSell_carname().equals("")){
            String[] carname = sellDto.getSell_carname().split("/");
            sellDto.setSell_carname(carname[0]);
            sellDto.setSell_cardept(carname[1]);
        }
        if (ss.updateSellCar(sellDto)){
            pw.print("<script>alert('修改成功');location.href='mywantsellcar.jsp'</script>");
        }else {
            pw.print("<script>alert('修改失败');history.go(-1)</script>");
        }
        pw.flush();
        pw.close();
    }

}

